package com.sxr.integration;

import java.util.Objects;

/**
 * 租户上下文
 * 存储当前请求的租户信息
 *
 * @author SXR
 * @since 1.0.0
 */
public class TenantContext {

    /** 租户ID */
    private final String tenantId;

    /** 租户名称 */
    private String tenantName;

    /** 租户密钥 */
    private String secretKey;

    /** 文件基础路径 */
    private String basePath;

    /** 白名单(直接放行的)的文件扩展名 */
    private String[] whiteExtensions;

    /** 允许的文件扩展名 */
    private String[] allowedExtensions;

    /** 禁止的文件扩展名 */
    private String[] forbiddenExtensions;


    /** 最大文件大小（字节） */
    private Long maxFileSize;

    /** 是否启用访问日志 */
    private boolean enableAccessLog;

    /** 是否启用限流 */
    private boolean enableRateLimit;

    /** 每秒最大请求数 */
    private Integer maxRequestsPerSecond;

    public TenantContext(String tenantId) {
        this.tenantId = tenantId;
    }


    /**
     * 检查文件扩展名是否被允许
     */
    public boolean isExtensionAllowed(String extension) {
        if (extension == null) {
            return false;
        }

        String lowerExt = extension.toLowerCase();

        // 如果有禁止列表，先检查是否在禁止列表中
        if (forbiddenExtensions != null) {
            for (String forbidden : forbiddenExtensions) {
                if (lowerExt.equals(forbidden.toLowerCase())) {
                    return false;
                }
            }
        }

        // 如果有允许列表，检查是否在允许列表中
        if (allowedExtensions != null) {
            for (String allowed : allowedExtensions) {
                if (lowerExt.equals(allowed.toLowerCase())) {
                    return true;
                }
            }
            return false; // 有允许列表但不在其中
        }

        return true; // 没有限制列表，默认允许
    }

    // Getter and Setter methods


    public String getTenantId() {
        return tenantId;
    }

    public String getTenantName() {
        return tenantName;
    }

    public void setTenantName(String tenantName) {
        this.tenantName = tenantName;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    public String[] getAllowedExtensions() {
        return allowedExtensions;
    }

    public void setAllowedExtensions(String[] allowedExtensions) {
        this.allowedExtensions = allowedExtensions;
    }

    public String[] getForbiddenExtensions() {
        return forbiddenExtensions;
    }

    public void setForbiddenExtensions(String[] forbiddenExtensions) {
        this.forbiddenExtensions = forbiddenExtensions;
    }

    public Long getMaxFileSize() {
        return maxFileSize;
    }

    public void setMaxFileSize(Long maxFileSize) {
        this.maxFileSize = maxFileSize;
    }

    public boolean isEnableAccessLog() {
        return enableAccessLog;
    }

    public void setEnableAccessLog(boolean enableAccessLog) {
        this.enableAccessLog = enableAccessLog;
    }

    public boolean isEnableRateLimit() {
        return enableRateLimit;
    }

    public void setEnableRateLimit(boolean enableRateLimit) {
        this.enableRateLimit = enableRateLimit;
    }

    public Integer getMaxRequestsPerSecond() {
        return maxRequestsPerSecond;
    }

    public void setMaxRequestsPerSecond(Integer maxRequestsPerSecond) {
        this.maxRequestsPerSecond = maxRequestsPerSecond;
    }

    public String[] getWhiteExtensions() {
        return whiteExtensions;
    }

    public void setWhiteExtensions(String[] whiteExtensions) {
        this.whiteExtensions = whiteExtensions;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        TenantContext that = (TenantContext) o;
        return Objects.equals(tenantId, that.tenantId);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(tenantId);
    }
}
